<?php

    class Zend_View_Helper_Api extends Zend_View_Helper_Partial
    {

        private $model;
        public function getFacebook($brand_id, $index){
            //indexes 2 , 10
            $model = new Application_Model_Common;
            $where = " feed_social_type = 'facebook' ";
            $order = " feed_social_id desc ";
            $limit = " $index,0 ";
            $data = $model->getAllRecordDetail(RKN_SOCIAL_FEEDS, "$where", $order,$limit);
            return $data[0];
        }
        
        public function getTwitter($brand_id, $index){
            //indexes 2, 10
            $model = new Application_Model_Common;
            $where = " feed_social_type = 'twitter' ";
            $order = " feed_social_id desc ";
            $limit = " $index,0 ";
            $data = $model->getAllRecordDetail(RKN_SOCIAL_FEEDS, "$where", $order,$limit);
            return $data[0];
        }
        
        public function getYoutube($brand_id, $index){
            //indexes 6
            $model = new Application_Model_Common;
            $where = " feed_social_type = 'youtube' ";
            $order = " feed_social_id desc ";
            $limit = " $index,0 ";
            $data = $model->getAllRecordDetail(RKN_SOCIAL_FEEDS, "$where", $order,$limit);
            return $data[0];
        }
        
        public function getInstagram($brand_id,$index){
            //indexes 9
            $model = new Application_Model_Common;
            $where = " feed_social_type = 'instagram' ";
            $order = " feed_social_id desc ";
            $limit = " $index,0 ";
            $data = $model->getAllRecordDetail(RKN_SOCIAL_FEEDS, "$where", $order,$limit);
            return $data[0];
        }
        
        public function resetIndexing($index){
            if($index > 12)
                $index = ($index%12);
            return $index;
            
        }
        
        public function render($data)
        {
            $newdata = array();
            $_fbcounter = 0;
            $_twcounter = 0;
            $_incounter = 0;
            $_ycounter = 0;
            $i = 1;
            
            foreach($data as $Key=>$Val){
                if($i == '1' || $i == '4' || $i == '7' || $i == '11' || $i == '12'){
                    $newdata[] = $Val;
                    $i = $this->resetIndexing(count($newdata)+1);
                }
                
                if($i == '2' || $i == '10'){ //facebook 
                    
                    $tmp = $this->getFacebook($Val['brand_id'], $_fbcounter);
                    $newdata[] = $tmp;
                    $_fbcounter++;
                    $i = $this->resetIndexing(count($newdata)+1);
                }
                
                if($i == '3' || $i == '5' || $i == '8'){ //twitter
                    
                    $tmp = $this->getTwitter($Val['brand_id'], $_twcounter);
                    $newdata[] = $tmp;
                    $_twcounter++;
                    $i = $this->resetIndexing(count($newdata)+1);
                }
                
                if($i == '6'){ //youtube
                    $tmp = $this->getYoutube($Val['brand_id'], $_ycounter);
                    $newdata[] = $tmp;
                    $_ycounter++;
                    $i = $this->resetIndexing(count($newdata)+1);
                }
                
                if($i == '9'){ //instagram
                    
                    $tmp = $this->getInstagram($Val['brand_id'], $_incounter);
                    $newdata[] = $tmp;
                    $_incounter++;
                    $i = $this->resetIndexing(count($newdata)+1);
                }
            }
            return $newdata;
        }
        
        
        
    }